package com.example.chapter5.demos.controller;

import com.example.chapter5.demos.entity.Student;
import com.example.chapter5.demos.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class StudentController {
    //通过id查找学生数据
//    @RequestMapping(value = "/students/{id}",method = RequestMethod.GET)
    @Autowired
    StudentMapper studentMapper;

    @GetMapping(value = "/students/{id}")
    public Object student(@PathVariable("id") int id) {
        return studentMapper.findById(id);
    }

    @PostMapping("/add")
    public Student student(Student student) {
        studentMapper.insert(student);
        return student;
    }
    @PutMapping("/put")
    public Student updateStudent(@RequestBody Student student) {
        studentMapper.updateById(student);
        return student;
    }

    @DeleteMapping("/students/{id}")
    public String deleteStudent(@PathVariable("id") int id) {
        studentMapper.deleteById(id);
        return "success";
    }
}